<html>
<head><meta charset="utf-8"><title>file_lstem · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/file_lstem.html">file_lstem</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="238076313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/file_lstem/near/238076313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Michael Hall <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/file_lstem.html#238076313">(May 10 2021 at 01:33)</a>:</h4>
<p>Firstly, apologies if this is not the correct place to ask this. </p>
<p>I would like to like to add a method to <code>std::path::Path</code> to extract the file stem, but for the portion before the <em>first</em> <code>.</code>. I was thinking to name it <code>file_lstem</code> so it is easy to recognise how it differs from the existing <a href="https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.file_stem"><code>file_stem</code></a> without needing to change the current API.<br>
I personally find that when I want a file stem it is almost always this "left" variant.<br>
Do you think this is something worth pursuing, and if so, do I create an RFC?</p>



<a name="238076747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/file_lstem/near/238076747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Michael Hall <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/file_lstem.html#238076747">(May 10 2021 at 01:42)</a>:</h4>
<p>I guess another option for the name could be <code>file_prefix</code></p>



<a name="238079211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/file_lstem/near/238079211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/file_lstem.html#238079211">(May 10 2021 at 02:30)</a>:</h4>
<p>Why does this need to be part of libstd directly? You could use <code>let bytes = path.file_name().as_bytes(); bytes[bytes.position('.')..]</code> I think.</p>



<a name="238079497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/file_lstem/near/238079497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Michael Hall <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/file_lstem.html#238079497">(May 10 2021 at 02:36)</a>:</h4>
<p>My thinking was that this is a fairly common need for people working with Path objects. As I mentioned, it is something I use far more than the <code>file_stem</code> variant.<br>
I appreciate that it is easy enough to do without a libstd method, but I guess the same statement could be made about many of the libstd methods?</p>



<a name="238079727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/file_lstem/near/238079727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/file_lstem.html#238079727">(May 10 2021 at 02:40)</a>:</h4>
<p>Not so sure about the name, but generally small additions like this are proposed as a PR and a member of the libs team will evaluate it there; no need to write a full RFC. Someone from the libs team may chime in here as well, of course.</p>



<a name="238080233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/file_lstem/near/238080233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/file_lstem.html#238080233">(May 10 2021 at 02:50)</a>:</h4>
<p>Some thoughts: How could this handle a <code>.foo</code> file?  Would it imply having an <code>lextension</code> too?  Or what about an iterator over the possible <code>(name, ext)</code> pairs?</p>



<a name="238080541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/file_lstem/near/238080541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/file_lstem.html#238080541">(May 10 2021 at 02:55)</a>:</h4>
<p>seems like you'd have trouble with something like <code>File Version 3.5.4 (Copy).tar.gz</code></p>



<a name="238081433"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/file_lstem/near/238081433" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Michael Hall <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/file_lstem.html#238081433">(May 10 2021 at 03:12)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116122">simulacrum</span> <a href="#narrow/stream/219381-t-libs/topic/file_lstem/near/238079727">said</a>:</p>
<blockquote>
<p>Not so sure about the name, but generally small additions like this are proposed as a PR and a member of the libs team will evaluate it there; no need to write a full RFC. Someone from the libs team may chime in here as well, of course.</p>
</blockquote>
<p>Thanks, I'll do that and also try and capture all of the discussion here so far.</p>



<a name="238081866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/file_lstem/near/238081866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Michael Hall <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/file_lstem.html#238081866">(May 10 2021 at 03:20)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/219381-t-libs/topic/file_lstem/near/238080233">said</a>:</p>
<blockquote>
<p>Some thoughts: How could this handle a <code>.foo</code> file?  Would it imply having an <code>lextension</code> too?  Or what about an iterator over the possible <code>(name, ext)</code> pairs?</p>
</blockquote>
<p>I was thinking of mirroring <code>file_stem</code> in this respect - i.e. if <code>.</code> if the file name starts with <code>.</code> and there is no other <code>.</code>, then return <code>None</code>.<br>
The idea of an iterator over <code>(name, ext)</code> pairs is interesting. Although, I guess that would be a more general method akin to <code>components</code>, I guess it would be <code>file_components</code>... One method from python's <code>pathlib</code> that  generalises over extensions is <a href="https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.suffixes"><code>suffixes</code></a>. So if we felt the need to have <code>lextension</code>, it might be better to just implement <code>extensions</code>, and then if others feel like <code>lextension</code> was needed it would be quite easy to add.</p>



<a name="238082043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/file_lstem/near/238082043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Michael Hall <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/file_lstem.html#238082043">(May 10 2021 at 03:23)</a>:</h4>
<p><span class="user-mention silent" data-user-id="229517">Jacob Lifshay</span> <a href="#narrow/stream/219381-t-libs/topic/file_lstem/near/238080541">said</a>:</p>
<blockquote>
<p>seems like you'd have trouble with something like <code>File Version 3.5.4 (Copy).tar.gz</code></p>
</blockquote>
<p>It would have trouble with that I guess. i.e. you wouldn't get <code>File Version 3.5.4 (Copy)</code> as a result.</p>



<a name="238085718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/file_lstem/near/238085718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/file_lstem.html#238085718">(May 10 2021 at 04:29)</a>:</h4>
<p><span class="user-mention silent" data-user-id="410803">Michael Hall</span> <a href="#narrow/stream/219381-t-libs/topic/file_lstem/near/238082043">said</a>:</p>
<blockquote>
<p>It would have trouble with that I guess. i.e. you wouldn't get <code>File Version 3.5.4 (Copy)</code> as a result.</p>
</blockquote>
<p>This is one of the kinds of things I was thinking of for the iterator -- you could take the first/last (depending which what it iterates) match of a known extension so that if <code>.tar.gz</code> was in the allow-list you'd get that one, not <code>.5.4 (Copy).tar.gz</code>, which isn't really ever of use to anyone.</p>



<a name="238234555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/file_lstem/near/238234555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Michael Hall <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/file_lstem.html#238234555">(May 11 2021 at 00:45)</a>:</h4>
<p>For anyone interested in following this: <a href="https://github.com/rust-lang/rust/pull/85166">https://github.com/rust-lang/rust/pull/85166</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>